﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// qt headers
#include <QScrollBar>
#include <QPainter>
#include <QMargins>
#include <QColor>
#include <QPaintEvent>

// custom headers
#include "EditingParametersTableView.h"
#include "EditingParametersTableModel.h"
#include "EditingParameterItemDelegate.h"
#include "EditingParametersTableHeaderView.h"
#include "EditingParameterItemSelectionModel.h"
#include "IParameter.gcs.h"
#include "PaintingUtils.h"
#include "GbmpNew.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs {
    EdittingParametersTableView::EdittingParametersTableView(QWidget *parent) : 
        QTableView(parent),
        m_rowDefaultHeight(24),
        m_horizontalHeaderHeight(24),
        m_border(1,1,1,1),
        m_borderRadius(0,0,0,0),
        m_borderColor(217, 221, 228, 255),
        m_sizeToContent(false)
    {
        Init();
    }
    
    EdittingParametersTableView::~EdittingParametersTableView()
    {
    }

    void EdittingParametersTableView::SetParameters(std::vector<std::shared_ptr<IParameter>> paramters)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pModel, L"m_pModel为空", L"GMEP", L"2024-01-30");
        m_pModel->SetParameters(paramters);
        emit ParametersChanged();

        if (m_sizeToContent)
        {
            int tableHeight =
                m_border.top() +
                m_horizontalHeaderHeight +
                m_border.bottom();
            
            DBG_WARN_AND_RETURN_VOID_UNLESS(verticalHeader(), L"verticalHeader()为空", L"GMEP", L"2024-01-30");
            DBG_WARN_AND_RETURN_VOID_UNLESS(model(), L"model()为空", L"GMEP", L"2024-01-30");
            for (int r = 0; r != model()->rowCount(); ++r)
            {
                tableHeight += verticalHeader()->sectionSize(r);
            }

            setFixedHeight(tableHeight);
            emit SizedToContent();
        }
    }

    void EdittingParametersTableView::SetSizeToContent(const bool & sizeToContent)
    {
        m_sizeToContent = sizeToContent;
    }

    void EdittingParametersTableView::SetBorder(const int left, const int top, const int right, const int bottom)
    {
        m_border = QMargins(left, top, right, bottom);
        setStyleSheet(GetStyleSheet());
    }

    void EdittingParametersTableView::paintEvent(QPaintEvent * event)
    {
        QPainter painter(this);

        painter.save();
        QStyleOption option;
        option.init(this);
        QStyle *pStyle = QTableView::style();
        pStyle->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
        painter.restore();

        QTableView::paintEvent(event);
    }

    void EdittingParametersTableView::Init()
    {
        m_pModel = NEW_AS_QT_CHILD(EdittingParametersTableModel,this);
        setModel(m_pModel);        

        EdittingParametersTableHeaderView *pHorizontalHeader = NEW_AS_QT_CHILD(EdittingParametersTableHeaderView,Qt::Orientation::Horizontal, this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pHorizontalHeader, L"pHorizontalHeader为空", L"GMEP", L"2024-01-30");
        setHorizontalHeader(pHorizontalHeader);

        EdittingParameterItemDelegate *pItemDelegate = NEW_AS_QT_CHILD(EdittingParameterItemDelegate,this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pItemDelegate, L"pItemDelegate为空", L"GMEP", L"2024-01-30");
        setItemDelegate(pItemDelegate);
        
        DBG_WARN_AND_RETURN_VOID_UNLESS(verticalHeader(), L"verticalHeader()为空", L"GMEP", L"2024-01-30");
        verticalHeader()->hide();
        verticalHeader()->setDefaultSectionSize(m_rowDefaultHeight);
        verticalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents);

        DBG_WARN_AND_RETURN_VOID_UNLESS(horizontalHeader(), L"horizontalHeader()为空", L"GMEP", L"2024-01-30");
        horizontalHeader()->setSectionResizeMode(COL_PARAMETER_NAME, QHeaderView::ResizeMode::Fixed);
        horizontalHeader()->setSectionResizeMode(COL_PARAMETER_VALUE, QHeaderView::ResizeMode::Stretch);
        horizontalHeader()->setSectionResizeMode(COL_PARAMETER_UNIT, QHeaderView::ResizeMode::Fixed);
        horizontalHeader()->setSectionResizeMode(COL_PARAMETER_INSTANCE_MARK, QHeaderView::ResizeMode::Fixed);
        horizontalHeader()->setFixedHeight(m_horizontalHeaderHeight);

        setColumnWidth(COL_PARAMETER_NAME, 144);
        setColumnWidth(COL_PARAMETER_UNIT, 66);
        setColumnWidth(COL_PARAMETER_INSTANCE_MARK, 56);
        
        setShowGrid(false);
        setEditTriggers(
            QAbstractItemView::EditTrigger::DoubleClicked |
            QAbstractItemView::EditTrigger::SelectedClicked |
            QAbstractItemView::EditTrigger::EditKeyPressed |
            QAbstractItemView::EditTrigger::CurrentChanged
        );
        //setFrameShape(QFrame::Shape::NoFrame);
        setStyleSheet(GetStyleSheet());
               
        //setMouseTracking(true);
        setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
        setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);

        DBG_WARN_AND_RETURN_VOID_UNLESS(verticalScrollBar(), L"verticalScrollBar()为空", L"GMEP", L"2024-01-30");
        verticalScrollBar()->setStyleSheet(
            "QScrollBar:vertical{"
                "width:8px;"
                "background:rgba(0,0,0,0%);"
                "margin:0px,0px,0px,0px;"
                "padding-top:0px;"
                "padding-bottom:0px;"
                "padding-left:1px;"
                "padding-right:1px;"
            "}"
            "QScrollBar::handle:vertical{"
                "width:6px;"
                "background:rgba(35,40,50,20%);"
                "border-radius:2px;"
                "min-height:20;"
            "}"
            "QScrollBar::handle:vertical:hover{"
                "width:6px;"
                "background:rgba(35,40,50,40%);"
                " border-radius:2px;"
                "min-height:20;"
            "}"
            "QScrollBar::add-line:vertical{"
                "height:0px;width:0px;"
                "border-image:url(:/images/icon_triangle_solid_down_8.png);"
                "subcontrol-position:bottom;"
            "}"
            "QScrollBar::sub-line:vertical{"
                "height:0px;width:0px;"
                "border-image:url(:/images/icon_triangle_solid_up_8.png);"
                "subcontrol-position:top;"
            "}"
            "QScrollBar::add-line:vertical:hover{"
                "height:0px;width:0px;"
                "border-image:url(:/images/icon_triangle_solid_down_8.png);"
                "subcontrol-position:bottom;"
            "}"
            "QScrollBar::sub-line:vertical:hover{"
                "height:0px;width:0px;"
                "border-image:url(:/images/icon_triangle_solid_up_8.png);"
                "subcontrol-position:top;"
            "}"
            "QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical{"
                "background:rgba(255,255,255,10%);"
                "border-radius:4px;"
            "}"
        );

        DBG_WARN_AND_RETURN_VOID_UNLESS(horizontalScrollBar(), L"horizontalScrollBar()为空", L"GMEP", L"2024-01-30");
        horizontalScrollBar()->setStyleSheet(
            "QScrollBar:horizontal{"
            "height:8px;"
            "background:rgba(0,0,0,0%);"
            "margin:0px,0px,0px,0px;"
            "padding-top:1px;"
            "padding-bottom:1px;"
            "padding-left:0px;"
            "padding-right:0px;"
            "}"
            "QScrollBar::handle:horizontal{"
            "height:6px;"
            "background:rgba(35,40,50,20%);"
            "border-radius:2px;"
            "min-width:20;"
            "}"
            "QScrollBar::handle:horizontal:hover{"
            "height:6px;"
            "background:rgba(35,40,50,40%);"
            "border-radius:2px;"
            "min-width:20;"
            "}"
            "QScrollBar::add-line:horizontal{"
            "height:0px;width:0px;"
            "border-image:url(:/images/icon_triangle_solid_down_8.png);"
            "subcontrol-position:left;"
            "}"
            "QScrollBar::sub-line:horizontal{"
            "height:0px;width:0px;"
            "border-image:url(:/images/icon_triangle_solid_up_8.png);"
            "subcontrol-position:right;"
            "}"
            "QScrollBar::add-line:horizontal:hover{"
            "height:0px;width:0px;"
            "border-image:url(:/images/icon_triangle_solid_down_8.png);"
            "subcontrol-position:left;"
            "}"
            "QScrollBar::sub-line:horizontal:hover{"
            "height:0px;width:0px;"
            "border-image:url(:/images/icon_triangle_solid_up_8.png);"
            "subcontrol-position:right;"
            "}"
            "QScrollBar::add-page:horizontal,QScrollBar::sub-page:horizontal{"
            "background:rgba(255,255,255,10%);"
            "border-radius:4px;"
            "}"
        );
    }

    QString EdittingParametersTableView::GetStyleSheet() const
    {
        return QString(
            "QTableView{"
            "background:transparent;"
            "border-left-width:%1px;border-top-width:%2px;border-right-width:%3px;border-bottom-width:%4px;"
            "border-top-left-radius:%5px;border-top-right-radius:%6px;border-bottom-right-radius:%7px;border-bottom-left-radius:%8px;"
            "border-color:rgba(%9, %10, %11, %12);"
            "border-style:solid;"
            "}")
            .arg(m_border.left()).arg(m_border.top()).arg(m_border.right()).arg(m_border.bottom())
            .arg(m_borderRadius.left()).arg(m_borderRadius.top()).arg(m_borderRadius.right()).arg(m_borderRadius.bottom())
            .arg(m_borderColor.red()).arg(m_borderColor.green()).arg(m_borderColor.blue()).arg(m_borderColor.alpha());
    }

}
